/*
 * TcpTest.cpp
 *
 *  Created on: Dec 31, 2013
 *      Author: slava
 */

#include <iostream>
#include "cm_net.h"
#include <fstream>
#include <sstream>

using std::cout;
using std::cin;
using std::endl;
using std::fstream;
using std::istringstream;

int main() {
	try {
		cout << "Setting up server" << endl;
		TcpListener tcp_server;
		tcp_server.listen();
		cout << "Server is up and listening" << endl;
		TcpChannel tcp_channel = tcp_server.accept();
		cout << "Server found client and TcpChannel is open" << endl;
		string filename("test_out.txt");
		fstream fd(filename.c_str(), std::fstream::out);
		cout << "Server waiting to recv" << endl;
		tcp_channel.read_into(fd);
		cout << "Server received, type your feedback " << endl;
		istringstream iss("Message delivered");
		tcp_channel.write_from(iss);
		cout << "Transfer complete. Quitting" << endl;
		fd.close();
	} catch (ConnectionError& e) {
		cout << e.what << " Terminating Server" << endl;
	}
	return 0;

}
